package cn.gdcp.materialdtest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

public class StuAdapter extends BaseAdapter {
    private ArrayList<Student> studentArrayList;
     private Context context;
     private IOnDelListener listener;
        public StuAdapter(Context context,ArrayList<Student> studentArrayList,IOnDelListener listener)  {
             this.context=context;
             this.studentArrayList=studentArrayList;
             this.listener=listener;
    }

    public void changeData(ArrayList<Student> studentArrayList) {
        this.studentArrayList=studentArrayList;
        notifyDataSetChanged();

    }
    public long getItemId(int position) {
        return position;
    }

    public int getCount() {
        return studentArrayList.size();
    }

    public Object getItem(int position) {

            return studentArrayList.get(position);
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
            if(view==null){
                LayoutInflater inflater=LayoutInflater.from(context);
                view=inflater.inflate(R.layout.studentitem,parent,false);
                 StuViewHolder holder=new StuViewHolder();
                 holder.textName=view.findViewById(R.id.student_name);
                 holder.del=view.findViewById(R.id.qx_img);
                 view.setTag(holder);
            }
            StuViewHolder holder=(StuViewHolder)view.getTag();

            holder.textName.setText(""+studentArrayList.get(position).getName()+"  "
                   + studentArrayList.get(position).getId()+"  "+studentArrayList.get(position).getAge() );

            holder.del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   listener.del(position);
                }
            });
                return view;
    }
}
